import QtQuick 1.0
import "AtndData.js" as AtndData

XmlListModelNG {
    id: root

    property int event_id: 0
    property string searchQuery
    property int countPerPage: 10
    property int __begin: 0
    property int __end: __begin + countPerPage
    property bool atEnd: status === XmlListModel.Ready && count !== __end - 1

//    source: root.searchQuery.length > 0 ? "http://api.atnd.org/events/?" + root.searchQuery + "&start=" + root.__begin + "&count=" + countPerPage : ''
    query: "/hash/events/event"

    property variant apiType: StateGroup {
        id: apiType
        states: [
            State {
                name: "event_id"
                when: event_id > 0
                PropertyChanges {
                    target: root
                    source: 'http://api.atnd.org/events/?event_id='.concat(event_id)
                    restoreEntryValues: true
                }
            },
            State {
                name: 'search'
                when: searchQuery.length > 0
                PropertyChanges {
                    target: root
                    source: "http://api.atnd.org/events/?" + root.searchQuery + "&start=" + root.__begin + "&count=" + countPerPage
                    restoreEntryValues: true
                }
            }

        ]
    }

    XmlRole { name: "event_id"; query: "event_id/number()"; isKey: true }
    XmlRole { name: "title"; query: "title/string()" }
    XmlRole { name: "catch_"; query: "catch/string()" }
    XmlRole { name: "description"; query: "description/string()" }
    XmlRole { name: "event_url"; query: "event_url/string()" }
    XmlRole { name: "started_at"; query: "started_at/string()" }
    XmlRole { name: "ended_at"; query: "ended_at/string()" }
    XmlRole { name: "url"; query: "url/string()" }
    XmlRole { name: "limit"; query: "limit/number()" }
    XmlRole { name: "address"; query: "address/string()" }
    XmlRole { name: "place"; query: "place/string()" }
    XmlRole { name: "lat"; query: "lat/number()" }
    XmlRole { name: "lon"; query: "lon/number()" }
    XmlRole { name: "owner_id"; query: "owner_id/number()" }
    XmlRole { name: "owner_nickname"; query: "owner_nickname/string()" }
    XmlRole { name: "owner_twitter_id"; query: "owner_twitter_id/string()" }
    XmlRole { name: "accepted"; query: "accepted/number()" }
    XmlRole { name: "waiting"; query: "waiting/string()" }
    XmlRole { name: "updated_at"; query: "updated_at/string()" }

    onSearchQueryChanged: {
        clear();
        __begin = 1;
    }

    onEvent_idChanged: {
        clear()
        __begin = 1
    }

    function push(item) { // override
        item.description = AtndData.str2description(item.description)
        append(item)
    }

    function more() {
        if (status === XmlListModel.Ready) {
            __begin += countPerPage
        }
    }
}
